package com.dgut.common.conf;

import com.dgut.common.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * 配置安全框架
 * 包括权限和认证
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)//权限注解
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationTokenFilter authenticationTokenFilter;

    /**
     * 密码的加密
     * 将BCryptPasswordEncoder注入容器
     */
    @Bean
    public PasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /**
     *传入密码和账户的bean
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    /**
     * 认证异常
     */
    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    /**
     * 授权
     */
    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    /**
     * 对登录进行放行
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 对于登录和注册接口 只能匿名访问
                .antMatchers("/user/login").permitAll()
                .antMatchers("/user/sign").anonymous()
                //后台的登录
                .antMatchers("/backend/login/{username}/{password}").anonymous()
                //展示全部创作全部可以访问
                .antMatchers("/literary/allLiterary").permitAll()
                //查询全部文章
                .antMatchers("/article/getAllArticle").permitAll()
                //文章的收藏数量
                .antMatchers("/article/collectNumber/{artId}").permitAll()
                //文件上传全部可以访问
                .antMatchers("/common/**").permitAll()
                //点赞全部可以访问
                .antMatchers("/literary/likeNumber/{litId}").permitAll()
                //文件上传
                .antMatchers("/picture/**").permitAll()
                .antMatchers("/file/**").permitAll()
                //swagger
                .antMatchers("/swagger-resources/**", "/webjars/**",
                        "/v2/**", "/swagger-ui.html/**").permitAll()
                //静态资源
                .antMatchers("/*.svg","/*.png","/*.js","/*.css","/*.html").permitAll()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();

        //将过滤器配置加入security框架,请求时候验证有没有登录
        http.addFilterBefore(authenticationTokenFilter,UsernamePasswordAuthenticationFilter.class);

        //配置异常处理器
        http.exceptionHandling()
                //认证失败
                .authenticationEntryPoint(authenticationEntryPoint)
                //授权失败
                .accessDeniedHandler(accessDeniedHandler);
        http.cors();

    }

}
